//
//  BasNavViewController.swift
//  Weibo
//
//  Created by apple on 15/12/22.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class BasNavViewController: UINavigationController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        //设置返回手势的代理
        self.interactivePopGestureRecognizer?.delegate = self
        // Do any additional setup after loading the view.
    }

    override func pushViewController(viewController: UIViewController, animated: Bool) {
        
        //viewController 是将要跳转的页面
        
        //自定义返回按钮
        //一旦自定义返回按钮 导航视图控制器的 返回手势的协议对象 会被重置
        if childViewControllers.count != 0 {
            let btn = UIButton(title: "返回", backImageName: nil, fontSize: 14, color: UIColor.lightGrayColor(), imageName: "navigationbar_back_withtext")
            btn.addTarget(self, action: "back", forControlEvents: .TouchUpInside)
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn)
            viewController.hidesBottomBarWhenPushed = true
        }
        
        //调用父类的跳转函数  这句话执行后才会真正跳转页面
        super.pushViewController(viewController, animated: animated)
    }
    
    
    @objc private func back() {
        //self就可以直接使用
        popViewControllerAnimated(true)
    }
    
    
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        print("你若安好,便是晴天")
//        if childViewControllers.count == 1 {
//            return false
//        }
//        return true
        
        return childViewControllers.count != 1
    }

}
